import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register/index'),
    hidden: true
  },
  {
    path: '/forget',
    component: () => import('@/views/forget/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '考试发布', icon: 'dashboard' }
    }]
  },

  {
    path: '/example',
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    meta: { title: '题库管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'select',
        name: 'select',
        component: () => import('@/views/table/select'),
        meta: { title: '单选题录入', icon: 'table' }
      },
      {
        path: 'multiple',
        name: 'multiple',
        component: () => import('@/views/table/multiple'),
        meta: { title: '多选题录入', icon: 'table' }
      },
      {
        path: 'determine',
        name: 'determine',
        component: () => import('@/views/table/determine'),
        meta: { title: '判断题录入', icon: 'table' }
      },
      {
        path: 'write',
        name: 'write',
        component: () => import('@/views/table/write'),
        meta: { title: '简答题录入', icon: 'table' }
      },
      {
        path: 'tree',
        name: 'Tree',
        component: () => import('@/views/tree/index'),
        meta: { title: '测试', icon: 'tree' }
      }
    ]
  },

  {
    path: '/juanku',
    component: Layout,
    redirect: '/juanku/chuangbian',
    name: 'juanku',
    meta: {
      title: '试卷管理',
      icon: 'tree'
    },
    children: [
      {
        path: 'chuangbian',
        component: () => import('@/views/juanku/chuangbian'), // Parent router-view
        name: 'chuangbian',
        meta: { title: '试卷列表' }
      },
      {
        path: 'index',
        component: () => import('@/views/juanku/index'),
        name: 'index',
        meta: { title: '试卷创编' }
      }
    ]
  },
  {
    path: '/form',
    component: Layout,
    redirect: '/form/menu1',
    name: 'form',
    meta: {
      title: '公告发布',
      icon: 'form'
    },
    children: [
      {
        path: 'menu1',
        component: () => import('@/views/form/menu1'), // Parent router-view
        name: 'Menu1',
        meta: { title: '发布公告' }
      },
      {
        path: 'menu2',
        component: () => import('@/views/form/menu2'),
        name: 'Menu2',
        meta: { title: '查看公告' }
      }
    ]
  },

  {
    path: '/nested',
    component: Layout,
    children: [{
      path: 'index',
      name: 'nested',
      component: () => import('@/views/nested/index'),
      meta: { title: '成绩统计', icon: 'nested' }
    }]
  },
  {
    path: '/modify',
    component: Layout,
    children: [{
      path: 'index',
      name: 'Modify',
      component: () => import('@/views/modify/index'),
      meta: { title: '考试判卷', icon: 'form' }
    }]
  },
  {
    path: '/exercise',
    component: Layout,
    redirect: '/exercise',
    name: 'Example',
    meta: { title: '智能训练', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'auxiliary',
        name: 'auxiliary',
        component: () => import('@/views/exercise/auxiliary'),
        meta: { title: '辅助训练', icon: 'table' }
      },
      {
        path: 'auxiliaryenter',
        name: 'auxiliaryenter',
        component: () => import('@/views/exercise/auxiliaryenter'),
        meta: { title: '考试', icon: 'el-icon-message-solid' },
        hidden: true // 在侧边栏隐藏这个入口
      },
      {
        path: 'random',
        name: 'random',
        component: () => import('@/views/exercise/random'),
        meta: { title: '随机训练', icon: 'table' }
      },
      {
        path: 'randomenter',
        name: 'randomenter',
        component: () => import('@/views/exercise/randomenter'),
        meta: { title: '考试', icon: 'el-icon-message-solid' },
        hidden: true // 在侧边栏隐藏这个入口
      },
      {
        path: 'bank',
        name: 'bank',
        component: () => import('@/views/exercise/bank'),
        meta: { title: '题库训练', icon: 'table' }
      },
      {
        path: 'bankenter',
        name: 'bankenter',
        component: () => import('@/views/exercise/bankenter'),
        meta: { title: '考试', icon: 'el-icon-message-solid' },
        hidden: true // 在侧边栏隐藏这个入口
      }
    ]
  },
  {
    path: '/exam',
    component: Layout,
    redirect: '/exam', // 默认重定向到考试规则页面
    meta: { title: '', icon: 'el-icon-message-solid' },
    children: [
      {
        path: 'index',
        name: 'exam',
        component: () => import('@/views/exam/index'),
        meta: { title: '在线考试', icon: 'el-icon-message-solid' }
      },
      {
        path: 'enter',
        name: 'enter',
        component: () => import('@/views/exam/enter'),
        meta: { title: '考试', icon: 'el-icon-message-solid' },
        hidden: true // 在侧边栏隐藏这个入口
      }
    ]
  },
  {
    path: '/feedback',
    component: Layout,
    children: [{
      path: 'index',
      name: 'feedback',
      component: () => import('@/views/feedback/index'),
      meta: { title: '信息反馈', icon: 'nested' }
    }]
  },
  {
    path: '/feedList',
    component: Layout,
    children: [{
      path: 'index',
      name: 'feedList',
      component: () => import('@/views/feedList/index'),
      meta: { title: '信息反馈', icon: 'nested' }
    }]
  },
  {
    path: '/manage',
    component: Layout,
    children: [{
      path: 'index',
      name: 'manage',
      component: () => import('@/views/manage/index'),
      meta: { title: '账号管理', icon: 'dashboard' }
    }]
  },
  {
    path: '/certificate',
    component: Layout,
    redirect: '/certificate', // 默认重定向到考试规则页面
    meta: { title: '所获证书', icon: 'nested' },
    children: [
      {
        path: 'look',
        name: 'look',
        component: () => import('@/views/certificate/look'),
        meta: { title: '查看证书', icon: 'el-icon-message-solid' }
      },
      {
        path: 'index',
        name: 'index',
        component: () => import('@/views/certificate/index'),
        meta: { title: '所获证书', icon: 'el-icon-message-solid' }
      }
    ]
  },
  {
    path: '/recept',
    component: Layout,
    redirect: '/recept', // 默认重定向到考试规则页面
    meta: { title: '反馈接收', icon: 'nested' },
    children: [
      {
        path: 'index',
        name: 'recept',
        component: () => import('@/views/recept/index'),
        meta: { title: '反馈列表', icon: 'el-icon-message-solid' }
      },
      {
        path: 'deal',
        name: 'recept',
        component: () => import('@/views/recept/deal'),
        meta: { title: '反馈处理', icon: 'el-icon-message-solid' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
